<?

  //////////////////////////////////////////////////////////////////
  // OrbitFAQ                                                     //
  // ---------                                                    //
  //                                                              //
  // Orbit FAQ was solely written and developed by Orbit Services //
  // http://www.orbitservices.net                                 //
  //                                                              //
  // Access the Forum here:                                       //
  // http://forums.orbitservices.net/index.php?c=4                //
  //                                                              //
  // OrbitFAQ utilises the following opensource projects/classes; //
  //  + Fckeditor - http://www.fckeditor.net                      //
  //  + Smarty Template Engine - http://smarty.php.net            //
  //  + Swift Email Class - http://www.swiftmailer.org/           //
  //  + OWASP PHP Filter Project - http://www.owasp.org           //
  //  + MySQL Search Class by Stephen Bartholomew                 //
  //                                                              //
  //////////////////////////////////////////////////////////////////

  // Get our other classes in one go here
  require('incs/smarty/Smarty.class.php');
  require('incs/sanitizept.inc.php');

    // Get our Config Settings
    $query_config = "SELECT * from orbitfaq_config";
    $result_config = $faqsql_query ($query_config)OR DIE( "$sql_query_error $query_config");;

    while($row_config = $faqsql_fetch_array($result_config))
      {

        $config_variable = $row_config[2];
        $config_value = $row_config[3];

        // Set our variables
        $$config_variable = $config_value;

      }

    $SkinFullPath = $absolute_path . $SkinPath;

    // Set our Logging based on DB
    if($logginglevel == 'ALL')
      {
        error_reporting(E_ERROR | E_CORE_ERROR | E_USER_ERROR | E_WARNING | E_CORE_WARNING | E_COMPILE_WARNING | E_USER_WARNING);
        $display_errors = 'On';
      }
    elseif($logginglevel == 'ERRORS')
      {
        error_reporting(E_ERROR | E_CORE_ERROR | E_USER_ERROR);
        $display_errors = 'On';
      }
    else // If is NONE or not set correctly will default to Off
      {
        $display_errors = 'Off';
      }

    ini_set('display_errors',$display_errors);


    // Get our Skins
    $dh = opendir($SkinFullPath);
    $i=1;
    while (($file = readdir($dh)) !== false) {
        if($file != "." && $file != "..") {
          $SkinListing[] = array("skin" => "$file");
        }
    }

    sort($SkinListing);

    // Set Default Skin
    if($_SESSION['orbitfaq_skin'] != '')
      {
        $defaultSkin = $_SESSION['orbitfaq_skin'];
      }

    // Get our current version
      $query_package = "SELECT * from orbitfaq_ovc WHERE `package` = 'orbitfaq' ORDER BY major DESC, minor DESC, servicepack DESC, beta DESC LIMIT 1";
      $result_package = $faqsql_query ($query_package)OR DIE( "$sql_query_error $query_login");;

      while($row_package = $faqsql_fetch_array($result_package))
        {
          $v_id = $row_package[0];
          $v_package = $row_package[1];
          $v_major = $row_package[2];
          $v_minor = $row_package[3];
          $v_servicepack = $row_package[4];
          $v_beta = $row_package[5];

          if($v_beta != '0')
            {
              $v_beta = " Beta $v_beta";
            }
          else
            {
              $v_beta = '';
            }

          $orbitfaq_version = $v_major . "." . $v_minor . "." . $v_servicepack . "" . $v_beta;

        }

  // REQUIRED SWIFT EMAIL CLASSED

  // Swift comes in two version, for PHP4 & PHP5.
  // We must work out which one to include for this server.

    // Get our version
    $phpv = phpversion();

    // Strip to the first character
    $phpv = substr($phpv,0,1);

    // Lets check the version and show an error if it does not fit our requirements
    if(($phpv != '4')AND($phpv != '5')AND($forceLoad != '1'))
      {
        echo "You do not have an up to date version of PHP, you are currently running $phpv+.<br />Please update to version 4 or 5 at <a href='http://ww.php.net'>www.php.net</a><br /><br />";
        echo "If you wish to force the loading of OrbitFAQ without swift email functionalities please set the forceLoad option in config.inc.php";
        exit;
      }
    elseif(($phpv == '4')OR($phpv == '5'))
      {
        $swift_folder = "swift_php" . $phpv;
        require("incs/$swift_folder/EasySwift.php");
      }

  // This function clears the Smarty Compiled Directory
  function checkIfLoggedIn()
    {

      if((!$_SESSION['orbitfaq_login'])OR(!$_SESSION['orbitfaq_fullname']))
        {
          session_register('orbitfaq_accesslevel');
          $_SESSION['orbitfaq_accesslevel'] = 'none';
          $orbitfaq_accesslevel = $_SESSION['orbitfaq_accesslevel'];
        }
      else
        {
          $orbitfaq_userid = $_SESSION['orbitfaq_userid'];
          $orbitfaq_login = $_SESSION['orbitfaq_login'];
          $orbitfaq_accesslevel = $_SESSION['orbitfaq_accesslevel'];
          $orbitfaq_fullname = $_SESSION['orbitfaq_fullname'];
        }

    }

  function highlight($haystack,$needle, $highlight_style)
    {
         $h=strtoupper($haystack);
         $n=strtoupper($needle);
         $pos=strpos($h,$n);
         if ($pos !== false)
             {
            $var=substr($haystack,0,$pos)."<font style='$highlight_style'>".substr($haystack,$pos,strlen($needle))."</font>";
            $var.=substr($haystack,($pos+strlen($needle)));
            $haystack=$var;
            }
         return $haystack;
    }

  function checkBanList($detail)
    {

      require('incs/config.inc.php');
      require('incs/db.inc.php');

      $query_banlist = "SELECT * from orbitfaq_banlist WHERE `detail` = '$detail'";
      $result_banlist = $faqsql_query ($query_banlist)OR DIE( "$sql_query_error $query_banlist");
      $count_banlist = $faqsql_count_rows($result_banlist);

      if($count_banlist != '0')
        {
          $valid = '0';
        }
      else
        {
          $valid = '1';
        }
      // Return the results
      return $valid;

    }


  // This function clears the Smarty Compiled Directory
  function changeSkin($skin)
    {

      session_register('orbitfaq_skin');
      $_SESSION['orbitfaq_skin'] = $skin;
      $defaultSkin = $_SESSION['orbitfaq_skin'];

      return $defaultSkin;

    }



  // This function clears the Smarty Compiled Directory
  function clearSmartyCompiled($path, $ClearSmartyCompiled)
    {
      if($ClearSmartyCompiled == '1')
        {
          $dh = opendir($path);
          $i=1;
          while (($file = readdir($dh)) !== false) {
              if($file != "." && $file != "..") {
                unlink("$path/$file");
              }
          }
        }
    }


  // This function shortens a string
  function ShortenText($text, $chars)
    {

      $text = $text." ";
      $text = substr($text,0,$chars);
      $text = substr($text,0,strrpos($text,' '));

      if($chars != "0")
        {
          $text = $text."...<br /><div id=\"right\" class=\"right mainmenu\">&raquo; read full answer</div>";
        }
        return $text;
    }




  // Function to Check the validity of Email addresses
  function checkEmail($email)
  {

     // Create the syntactical validation regular expression
     $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";

     // Presume that the email is invalid
     $valid = 0;

     // Validate the syntax
     if (eregi($regexp, $email))
      {

        // Check if the getmxrr function is available
        if (function_exists('getmxrr'))
          {

            // Split the email address with its domain
            list($username,$domaintld) = split("@",$email);

            // Validate the domain
            if (getmxrr($domaintld,$mxrecords))
              {
                $valid = 1;
              }
            else
              {
                $valid = 0;
              }

          }
        else
          {

           // Because the function is not available trust our
           // Primary Validation on user@domain.com

            $valid = 1;

          }

      }

    return $valid;

  }

  function makeRandomPassword() {
    $salt = "abchefghjkmnpqrstuvwxyz0123456789";
    srand((double)microtime()*1000000);
      $i = 0;
      while ($i <= 7) {
          $num = rand() % 33;
          $tmp = substr($salt, $num, 1);
          $pass = $pass . $tmp;
          $i++;
      }
      return $pass;
  }


#############################################################
#
# -=[ MySQL Search Class ]=-
#
#      version 1.5
#
# (c) 2002 Stephen Bartholomew
#
# Functionality to search through a MySQL database, across
# all columns, for multiple keywords
#
# Usage:
#
#    Required:
#        $mysearch = new MysqlSearch;
#        $mysearch->setidentifier("MyPrimaryKey");
#        $mysearch->settable("MyTable");
#        $results_array = $mysearch->find($mysearchterms);
#
#    Optional:
#        This will force the columns that are searched
#        $mysearch->setsearchcolumns("Name, Description");
#
#             Set the ORDER BY attribute for SQL query
#            $mysearch->setorderby("Name");
#
##############################################################

class MysqlSearch
{
    function find($keywords)
    {
        $keywords_array = explode(" ",$keywords);
        if(!$this->searchcolumns)
        {
            $this->searchcolumns = "*";
            $search_data_sql = "SELECT ".$this->searchcolumns." FROM ".$this->table;
        }
        else
        {
            $search_data_sql = "SELECT ".$this->entry_identifier.",".$this->searchcolumns." FROM ".$this->table;
        }
        $search_data_ref = mysql_query($search_data_sql);
        $search_results_array = array();
        if($search_data_ref)
        {
            while($all_data_array = mysql_fetch_array($search_data_ref))
            {
                $my_ident = $all_data_array[$this->entry_identifier];
                foreach($all_data_array as $entry_key=>$entry_value)
                {
                    foreach($keywords_array as $keyword)
                    {
                        if($keyword)
                        {
                            if(stristr($entry_value,$keyword))
                            {
                                $keywords_found_array[$keyword]++;
                            }
                        }
                        else
                        {
                            $keywords_found_array[$keyword]++;
                        }
                        unset($keyword);
                    }
                    if(sizeof($keywords_found_array) == sizeof($keywords_array))
                    {
                        array_push($search_results_array,"$my_ident");
                        break;
                    }
                }
                unset($keywords_found_array);
                unset($entry_key);
                unset($entry_value);
            }
        }
        $this->numresults = sizeof($search_results_array);
        return $search_results_array;
    }
    function setidentifier($entry_identifier)
    {
        $this->entry_identifier = $entry_identifier;
    }
    function settable($table)
    {
        $this->table = $table;
    }
    function setsearchcolumns($columns)
    {
        $this->searchcolumns = $columns;
    }
}

/*
  // Lets Make Sure Our $_GETs are passed correctly
  if(isset($_GET))
    {

      foreach ( $_GET as $key=>$value )
      {

        //$$key = $value;

      }

    }

*/
?>